import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [{
  path: '/login',
  component: () => import('@/views/login/index'),
  hidden: true
},

{
  path: '/404',
  component: () => import('@/views/404'),
  hidden: true
},

{
  path: '/',
  component: Layout,
  redirect: '/dashboard/index',
  name: 'Dashboard',
  meta: {
    title: '首页',
    icon: 'dashboard'
  },
  children: [{
    path: '/dashboard/index',
    name: '首页',
    component: () => import('@/views/dashboard/index'),
    meta: {
      title: '首页',
      icon: 'el-icon-s-home'
    }
  }]
},

{
  path: '/user',
  component: Layout,
  redirect: '/user/index',
  children: [{
    path: 'index',
    component: () => import('@/views/user/index'),
    meta: {
      title: '个人中心',
      icon: 'el-icon-user'
    }
  }]
},

]
// 异步路由
export const asyncRoutes = [
  // {
  //   path: '/roles',
  //   component: Layout,
  //   meta: {
  //     title: '管理员权限',
  //     role: ['admin'],
  //     icon: 'admin-manage'
  //   },
  //   children: [{
  //     path: 'user',
  //     name: 'User',
  //     component: () => import('@/views/role/user'),
  //     meta: {
  //       title: '用户管理',
  //       icon: 'user',
  //       role: ['admin']
  //     }
  //   },
  //   {
  //     path: 'role',
  //     name: 'Role',
  //     component: () => import('@/views/role/role'),
  //     meta: {
  //       title: '权限管理',
  //       icon: 'roles',
  //       role: ['admin']
  //     }
  //   },

  //   ]
  // },
  // 账号管理
  {
    path: '/account',
    component: Layout,
    redirect: '/account/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/account/index'),
        name: 'Account',
        meta: { title: '账号管理', icon: 'el-icon-s-tools', noCache: true, role: ['admin'] }
      }
    ]
  },
  // 生活区管理
  {
    path: '/living',
    component: Layout,
    redirect: '/living/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/living/index'),
        name: 'Living',
        meta: { title: '生活区管理', icon: 'el-icon-s-help', noCache: true, role: ['admin'], }
      }
    ]
  },
  // 学生信息管理
  {
    path: '/studentInfo',
    component: Layout,
    redirect: '/studentInfo/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/studentInfo/index'),
        name: 'StudentInfo',
        meta: { title: '学生信息管理', icon: 'el-icon-tickets', noCache: true, role: ['admin'], }
      }
    ]
  },
  // 宿管信息管理
  {
    path: '/housemaster',
    component: Layout,
    redirect: '/housemaster/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/housemaster/index'),
        name: 'Housemaster',
        meta: { title: '宿管信息管理', icon: 'el-icon-tickets', noCache: true, role: ['admin'], }
      }
    ]
  },
  // 楼栋管理
  {
    path: '/building',
    component: Layout,
    redirect: '/building/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/building/index'),
        name: 'Building',
        meta: { title: '楼栋管理', icon: 'el-icon-office-building', noCache: true, role: ['admin'], }
      }
    ]
  },
  // 房间管理
  {
    path: '/room',
    component: Layout,
    redirect: '/room/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/room/index'),
        name: 'Room',
        meta: { title: '房间管理', icon: 'el-icon-s-home', noCache: true, role: ['admin'], }
      }
    ]
  },

  // 寝室管理
  {
    path: '/dormitory',
    component: Layout,
    redirect: '/dormitory/index',
    meta: { title: '寝室管理', icon: 'el-icon-s-order', role: ['admin', 'housemaster'], },
    children: [
      {
        path: 'index',
        component: () => import('@/views/dormitory/index'),
        name: 'Dormitory',
        meta: { title: '寝室管理', icon: 'el-icon-s-order', noCache: true, role: ['admin', 'housemaster'], },
      },
      {
        path: '/dormitory/in',
        name: "in",
        component: () => import('@/views/dormitory/in'),
        meta: { title: '入住', noCache: true, role: ['admin', 'housemaster'] },
        hidden: true
      },
      {
        path: '/dormitory/out',
        name: "out",
        component: () => import('@/views/dormitory/out'),
        meta: { title: '退住', noCache: true, role: ['admin', 'housemaster'], },
        hidden: true
      },
    ]
  },
  // 水电费管理
  {
    path: '/utilitybill',
    component: Layout,
    redirect: '/utilitybill/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/utilitybill/index'),
        name: 'Utilitybill',
        meta: { title: '水电费管理', icon: 'el-icon-set-up', noCache: true, role: ['housemaster', 'student'] }
      }
    ]
  },
  // 维修上报
  {
    path: '/repairescalation',
    component: Layout,
    redirect: '/repairescalation/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/repairescalation/index'),
        name: 'Repairescalation',
        meta: { title: '维修上报', icon: 'el-icon-warning', noCache: true, role: ['housemaster', 'student'] }
      }
    ]
  },
  // 失物招领
  {
    path: '/lostfound',
    component: Layout,
    redirect: '/lostfound/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/lostfound/index'),
        name: 'Lostfound',
        meta: { title: '失物招领', icon: 'el-icon-guide', noCache: true, role: ['housemaster', 'student'] }
      }
    ]
  },
  // 门禁记录
  {
    path: '/access',
    component: Layout,
    redirect: '/access/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/access/index'),
        name: 'Access',
        meta: { title: '门禁记录', icon: 'el-icon-lock', noCache: true, role: ['housemaster', 'student'] }
      }
    ]
  },
  // 防疫管理
  {
    path: '/epidemicprevention',
    component: Layout,
    redirect: '/epidemicprevention/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/epidemicprevention/index'),
        name: 'Epidemicprevention',
        meta: { title: '防疫管理', icon: 'el-icon-s-marketing', noCache: true, role: ['housemaster', 'student'] }
      }
    ]
  },
  // 卫生检查
  {
    path: '/hygiene',
    component: Layout,
    redirect: '/hygiene/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/hygiene/index'),
        name: 'Hygiene',
        meta: { title: '卫生检查', icon: 'el-icon-s-claim', noCache: true, role: ['housemaster', 'student'] }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  {
    path: '*',
    redirect: '/404',
    hidden: true
  }
]
const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({
    y: 0
  }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
